package com.cskaoyan.service.home;

import com.cskaoyan.bean.promotion.GrouponExample;
import com.cskaoyan.bean.promotion.GrouponRules;
import com.cskaoyan.bean.promotion.GrouponRulesExample;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.system.ConfigService;
import com.cskaoyan.vo.config.ConfigWxVo;
import com.cskaoyan.vo.goods.WxGoodsListSonVo;
import com.cskaoyan.vo.home.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:houzhitao@139.com
 */
@Service
public class HomeIndexServiceImpl implements HomeIndexService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    ConfigService configService;

    @Override
    public WxHomeIndexVo homeIndex() {
        //首先查询微信配置
        ConfigWxVo configWxVo = configService.queryWxConfig();

        //根据is_New查询goods
        Integer newIndex = Integer.parseInt(configWxVo.getCskaoyanmall_wx_index_new());
        List<WxGoodsListSonVo> newGoodsList = goodsMapper.selectByIsNew(newIndex);

        //查询所有的coupon信息
        List<WxHomeIndexSonCouponVo> couponList = couponMapper.selectAll();

        //在category中查询pid为0的信息
        List<WxHomeIndexSonCategoryVo> channel = categoryMapper.selectChannelByPid();

        //在groupon_rules结合goods表中查询
        List<WxHomeIndexSonGrouponVo> grouponList = new ArrayList<>();
        //首先遍历出groupon表中信息
        GrouponRulesExample grouponExample = new GrouponRulesExample();
        grouponExample.createCriteria().andDeletedEqualTo(false);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponExample);
        //其次遍历查出信息并且根据goods_id查询goods信息
        for (GrouponRules grouponRule : grouponRules) {
            WxHomeIndexSonGrouponVo wxHomeIndexSonGrouponVo = new WxHomeIndexSonGrouponVo();
            //查询商品信息并给goods赋值
            WxHomeIndexSonGrouponSonGoodsVo wxHomeIndexSonGrouponSonGoodsVo = goodsMapper.selectByGroupon(grouponRule.getGoodsId());
            wxHomeIndexSonGrouponVo.setGoods(wxHomeIndexSonGrouponSonGoodsVo);
            //给member赋值
            wxHomeIndexSonGrouponVo.setGroupon_member(grouponRule.getDiscountMember());
            //给price赋值
            wxHomeIndexSonGrouponVo.setGroupon_price(wxHomeIndexSonGrouponSonGoodsVo.getRetailPrice().subtract(grouponRule.getDiscount()));
            //最后添加到grouponList中
            grouponList.add(wxHomeIndexSonGrouponVo);
        }

        //在ad中查询
        List<WxHomeIndexSonAdVo> banner = adMapper.selectAll();

        //在brand表中查询
        Integer brandIndex = Integer.parseInt(configWxVo.getCskaoyanmall_wx_index_brand());
        List<WxHomeIndexSonBrandVo> brandList = brandMapper.selectAll(brandIndex);

        //根据是否是热门查询goods
        Integer hotIndex = Integer.parseInt(configWxVo.getCskaoyanmall_wx_index_hot());
        List<WxGoodsListSonVo> hotGoodsList = goodsMapper.selectByIsHot(hotIndex);

        //topic表中查询
        List<WxHomeIndexSonTopicVo> topicList = topicMapper.selectAll();

        //根据每一个channel作为pid查询category
        List<WxHomeIndexSonFloorGoodsVo> floorGoodsList = new ArrayList<>();
        for (WxHomeIndexSonCategoryVo category : channel) {
            WxHomeIndexSonFloorGoodsVo wxHomeIndexSonFloorGoodsVo = new WxHomeIndexSonFloorGoodsVo();
            //赋值id
            wxHomeIndexSonFloorGoodsVo.setId(category.getId());
            //赋值name
            wxHomeIndexSonFloorGoodsVo.setName(category.getName());
            //将id作为pid在表中联合goods的category_id查询goods信息
            List<WxGoodsListSonVo> goodsList = goodsMapper.selectUnionGoodsAndCategoryByPid(category.getId());
            wxHomeIndexSonFloorGoodsVo.setGoodsList(goodsList);
        }

        //将上述查询的9个值赋值给要返回的参数的成员变量
        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo();
        wxHomeIndexVo.setNewGoodsList(newGoodsList);
        wxHomeIndexVo.setCouponList(couponList);
        wxHomeIndexVo.setChannel(channel);
        wxHomeIndexVo.setGrouponList(grouponList);
        wxHomeIndexVo.setBanner(banner);
        wxHomeIndexVo.setBrandList(brandList);
        wxHomeIndexVo.setHotGoodsList(hotGoodsList);
        wxHomeIndexVo.setTopicList(topicList);
        wxHomeIndexVo.setFloorGoodsList(floorGoodsList);

        return wxHomeIndexVo;

    }
}
